var t = require("../../../@babel/runtime/helpers/interopRequireDefault"), e = require("../../../@babel/runtime/helpers/defineProperty"), a = require("../../../@babel/runtime/helpers/objectSpread2"), o = t(require("../../../utils/util.js")), s = getApp();

Page({
    data: {
        isLoadMore: !0,
        page: 1,
        total_page: 0,
        id: 0,
        store_id: 0,
        disableds: !1,
        titleText: "创建活动",
        kwd: "",
        show: !1,
        showSearchGood: !1,
        listOne: [],
        start_time: "",
        end_time: "",
        cat_level: 2,
        category1: 0,
        category2: 0,
        category1_disabled: !1,
        category2_disabled: !0,
        categoryOption1: [ {
            text: "不限分类",
            value: 0
        } ],
        categoryOption2: [ {
            text: "不限分类",
            value: 0,
            child: []
        } ],
        activeCategoryID: "",
        minDate: new Date().getTime(),
        showStartTime: !1,
        currentStartTime: "",
        showEndTime: !1,
        currentEndTime: "",
        goodradio: "",
        tab: "all",
        new_url: !1,
        step: 1,
        has_option: 0,
        search_form: {},
        search_goods: {},
        category_list: [],
        goods_list: [],
        list: [],
        keywords: "",
        form: {},
        fight_groups_lottery_set: "",
        data: "",
        has_one_goods: "",
        is_edit: "",
        goods_total: 0,
        goods_per_size: 0,
        goods_current_page: 0,
        coupon_result: [],
        coupon_show: !1,
        coupon_keyword: "",
        coupon_list: [],
        love_name: "",
        loves: "",
        is_open_integral: "",
        store_show: !1,
        store_list: [],
        store_keyword: "",
        store_obj: "",
        loading: !1,
        table_loading: !1,
        rules: {
            title: {
                required: !0,
                message: "请输入活动标题"
            }
        },
        startPos: void 0
    },
    onLoad: function(t) {
        t.store_id && (this.data.store_id = t.store_id), t.id ? (this.setData({
            disableds: !0
        }), this.data.id = t.id, this.editData()) : this.initFun();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    initFun: function(t) {
        var e = t || {
            fight_groups_lottery: !1,
            keywords: {
                share_title: [ "拼团活动名称", "商品最高价", "商品最低价" ],
                share_content: [ "拼团活动名称", "商品最高价", "商品最低价" ],
                desc_title: [ "团长名称", "拼团时间倒计时", "拼团活动名称", "商品最高价", "商品最低价", "拼团人数" ],
                desc_content: [ "团长名称", "拼团时间倒计时", "拼团活动名称", "商品最高价", "商品最低价", "拼团人数" ]
            },
            loves: !0,
            love_name: "爱心值",
            is_open_integral: !1
        }, o = e.keywords, s = e.love_name ? e.love_name : "", i = !!e.loves && e.loves, n = !!e.fight_groups_lottery_set && e.fight_groups_lottery_set, r = e.fight_groups_lottery_info ? e.fight_groups_lottery_info : {}, d = !!e.is_open_integral && e.is_open_integral, l = e.has_one_store_cashier ? e.has_one_store_cashier : {};
        1 == n.open_type && null == r.is_open && (r.is_open = 1), console.log(r.is_separate_rules);
        var _ = a({
            goods_id: "",
            goods_name: "",
            is_lead: 0,
            barrage: 0,
            hide_num: "1",
            setting: "",
            share_title: "原价￥<<商品最高价>>，现价￥<<商品最低价>>起，参加<<拼团活动名称>>即可享受",
            share_content: "活动商品数量有限，先到先得。",
            desc_title: "我已经参加了<<团长名称>>发起的拼团。",
            desc_content: "凑足<<拼团人数>>人即可享受拼团优惠，我已经参加，你也来吧。",
            has_many_level: [],
            has_many_options: [],
            is_love: 0,
            love: "",
            is_balance: 0,
            balance: "",
            is_coupon: 0,
            fight_groups_lottery_info: r,
            coupon: [],
            business_type: 1,
            store_id: this.data.store_id,
            start_time: "",
            end_time: ""
        }, e), h = {}, u = !1;
        _.id && (_.start_time && (_.start_time = parseInt(1e3 * _.start_time)), _.end_time && (_.end_time = parseInt(1e3 * _.end_time)), 
        h = a({}, _.has_one_goods), 1 != _.status && 0 != _.status || (u = !0)), this.setData({
            keywords: o,
            form: _,
            fight_groups_lottery_set: n,
            data: e,
            has_one_goods: h,
            is_edit: u,
            love_name: s,
            loves: i,
            is_open_integral: d,
            store_obj: l
        });
    },
    showTextPopup: function() {
        wx.showModal({
            title: "提示",
            showCancel: !1,
            content: "勾选此选项则手机端商品详情页底部仅展示“去参团”，不展示“一键开团”",
            success: function(t) {
                t.confirm && console.log("用户点击确定");
            }
        });
    },
    showTextPopup1: function() {
        wx.showModal({
            title: "提示",
            showCancel: !1,
            content: "参与过该活动一次的视为老用户，老用户只允许开团不允许参团。此场景适合设置特价商品，用于鼓励老用户开团，协助店铺拉新。",
            success: function(t) {
                t.confirm && console.log("用户点击确定");
            }
        });
    },
    editData: function() {
        var t = this, e = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.edit");
        s._getNetWork({
            url: e,
            data: {
                store_id: t.data.store_id,
                id: t.data.id
            },
            success: function(e) {
                var a = e.data;
                1 == a.result ? (t.initFun(a.data), t.setData({
                    start_time: o.default.formatTimeTwo(a.data.start_time, "Y/M/D h:m:s"),
                    currentStartTime: 1e3 * a.data.start_time,
                    currentEndTime: 1e3 * a.data.end_time
                }), a.data.end_time && t.setData({
                    end_time: o.default.formatTimeTwo(a.data.end_time, "Y/M/D h:m:s")
                })) : wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    onChangetitle: function(t) {
        this.data.form.title = t.detail;
    },
    getCategoryOne: function() {
        if (!this.data.disableds) if (this.data.listOne.length > 0) this.setData({
            showSearchGood: !0
        }); else {
            var t = this, e = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.get-category");
            s._getNetWork({
                url: e,
                data: {
                    store_id: t.data.store_id
                },
                success: function(e) {
                    var a = e.data;
                    if (1 == a.result) {
                        var o = a.data, s = t.data.categoryOption1;
                        o.length > 0 && (o.forEach(function(t, e) {
                            s.push({
                                text: t.name,
                                value: t.id
                            });
                        }), t.getChildCate(1, "", !1)), t.setData({
                            categoryOption1: s,
                            showSearchGood: !0
                        });
                    } else wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onChangeCate: function(t) {
        this.setData({
            kwd: t.detail
        });
    },
    getChildCate: function() {
        var t = this, e = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.search-goods");
        s._getNetWork({
            url: e,
            data: {
                store_id: t.data.store_id,
                category: t.data.activeCategoryID,
                goods_name: t.data.kwd,
                page: t.data.page
            },
            success: function(e) {
                var a = e.data;
                1 == a.result ? t.setData({
                    listOne: a.data.data,
                    isLoadMore: a.data.last_page > 1,
                    total_page: a.data.last_page
                }) : wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getMore: function() {
        this.data.page += 1;
        var t = this, e = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.search-goods");
        s._getNetWork({
            url: e,
            data: {
                store_id: t.data.store_id,
                category: t.data.activeCategoryID,
                goods_name: t.data.kwd,
                page: t.data.page
            },
            success: function(e) {
                var a = e.data;
                if (1 == a.result) {
                    var o = t.data.listOne.concat(a.data.data);
                    t.setData({
                        listOne: o,
                        isLoadMore: !(t.data.page >= a.data.total_page)
                    });
                } else wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    cloneSearchGood: function() {
        this.setData({
            showSearchGood: !1
        });
    },
    selectCate1: function(t) {
        var e = t.detail;
        this.data.kwd = "", e ? (this.data.activeCategoryID = e, this.getCategorytwo(e, !0), 
        this.getChildCate()) : (console.log("不限一级分类"), this.initData(), this.data.activeCategoryID = "", 
        this.getChildCate());
    },
    selectCate2: function(t) {
        var e = t.detail;
        e ? (this.data.activeCategoryID = e, this.getChildCate()) : (console.log("不限二级分类"), 
        this.data.activeCategoryID = this.data.category1, this.getChildCate());
    },
    getCategorytwo: function(t, e) {
        var a = this;
        if (this.setData({
            categoryOption2: [ {
                text: "不限分类",
                value: 0,
                child: []
            } ],
            category2: 0,
            category2_disabled: !0
        }), 0 != t) {
            var o = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.get-category-json");
            s._getNetWork({
                url: o,
                data: {
                    store_id: a.data.store_id,
                    parent_id: t,
                    level: 2
                },
                success: function(t) {
                    var o = t.data;
                    if (1 == o.result) {
                        var s = response.data, i = a.data.categoryOption2;
                        s.length > 0 && (s.forEach(function(t, e) {
                            i.push({
                                text: t.name,
                                value: t.id
                            });
                        }), a.setData({
                            categoryOption2: i
                        }), e && (a.setData({
                            category2_disabled: !1
                        }), a.selectComponent("#categoryShow2").toggle()));
                    } else wx.showToast({
                        title: o.msg,
                        icon: "none",
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    initData: function() {
        this.setData({
            kwd: "",
            isLoadMore: !0,
            page: 1,
            total_page: 0,
            listOne: [],
            cat_level: 2,
            category1: 0,
            category2: 0,
            category1_disabled: !1,
            category2_disabled: !0,
            categoryOption2: [ {
                text: "不限分类",
                value: 0,
                child: []
            } ]
        });
    },
    clickGoodRadio: function(t) {
        var a, o, s = this, i = t.currentTarget.dataset.item, n = t.currentTarget.dataset.index;
        if (!(i.has_many_activity_goods_count >= 1)) {
            this.setData((e(a = {
                goodradio: n
            }, "has_one_goods.title", i.title), e(a, "has_one_goods.id", i.id), e(a, "has_one_goods.thumb", i.thumb), 
            e(a, "has_one_goods.price", i.price), e(a, "has_one_goods.stock", i.stock), e(a, "has_one_goods.sku", i.sku), 
            a)), this.data.new_url = !1, this.data.has_option = i.has_option, this.data.has_option ? this.setData(e({}, "form.has_many_options", i.has_many_options)) : this.setData(e({}, "form.has_many_options", [])), 
            this.data.has_option ? this.data.form.has_many_options.forEach(function(t, e) {
                s.data.form.has_many_options[e].option_price = t.product_price, s.data.form.has_many_options[e].option_id = t.id, 
                s.data.form.has_many_options[e].option_title = t.title, s.data.form.has_many_options[e].has_many_option_level = [], 
                s.data.form.has_many_options[e].buy_limit = "0", s.data.form.has_many_options[e].stock = t.stock;
            }) : this.data.form.has_many_options.push({
                option_title: "单规格",
                option_price: i.price,
                stock: i.stock,
                buy_limit: "0",
                has_many_option_level: []
            }), console.log(this.data.form.has_many_options);
            var r = this.data.form;
            this.setData((e(o = {
                form: r
            }, "form.goods_id", this.data.has_one_goods.id), e(o, "form.goods_name", this.data.has_one_goods.title), 
            e(o, "showSearchGood", !1), o));
        }
    },
    showTime: function(t) {
        var e = t.currentTarget.dataset.index;
        this.data.disableds || (1 == e && this.setData({
            showStartTime: !0
        }), 2 == e && this.setData({
            showEndTime: !0
        }));
    },
    confirmStartTime: function(t) {
        this.setData(e({
            showStartTime: !1,
            start_time: o.default.formatTimeTwo(t.detail / 1e3, "Y/M/D h:m:s")
        }, "form.start_time", parseInt(t.detail / 1e3)));
    },
    confirmEndtTime: function(t) {
        this.setData(e({
            showEndTime: !1,
            end_time: o.default.formatTimeTwo(t.detail / 1e3, "Y/M/D h:m:s")
        }, "form.end_time", parseInt(t.detail / 1e3)));
    },
    cancelStartTime: function() {
        this.setData(e({
            showStartTime: !1,
            start_time: ""
        }, "form.start_time", ""));
    },
    cancelEndtTime: function() {
        this.setData(e({
            showEndTime: !1,
            end_time: ""
        }, "form.end_time", ""));
    },
    addTier: function() {
        if (this.data.form.has_many_level.length >= 3) wx.showToast({
            title: "最多可设置3个阶梯",
            icon: "none",
            duration: 1e3
        }); else {
            var t = this.data.form.has_many_level;
            t.push({
                level_num: this.data.form.has_many_level.length + 1,
                member_num: "",
                expire_in: ""
            }), this.setData(e({}, "form.has_many_level", t));
        }
    },
    delTier: function(t) {
        var a = t.currentTarget.dataset.index, o = this.data.form.has_many_level;
        o.splice(a, 1), o.forEach(function(t, e) {
            t.level_num = e + 1;
        }), this.setData(e({}, "form.has_many_level", o));
    },
    changeLevelInput: function(t) {
        var a = t.currentTarget.dataset.index;
        "member_num" == t.currentTarget.dataset.tag ? this.setData(e({}, "form.has_many_level[" + a + "].member_num", t.detail.value)) : this.setData(e({}, "form.has_many_level[" + a + "].expire_in", t.detail.value));
    },
    onChangeMenu: function(t) {
        this.setData(e({}, "form.menu_show", t.detail));
    },
    inputHideNum: function(t) {
        this.setData(e({}, "form.hide_num", t.detail));
    },
    onChangeCoupon: function(t) {
        this.setData(e({}, "form.is_coupon", t.detail));
    },
    openCoupon: function() {
        this.searchCoupon(1);
    },
    onChangeCouInput: function(t) {
        this.setData({
            coupon_keyword: t.detail
        });
    },
    searchCoupon: function(t) {
        var e = this;
        if (1 == t && e.data.coupon_list.length > 0) this.setData({
            coupon_show: !0
        }); else {
            var a = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.coupon");
            s._getNetWork({
                url: a,
                data: {
                    store_id: e.data.store_id,
                    keyword: e.data.coupon_keyword
                },
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? e.setData({
                        coupon_list: a.data,
                        coupon_show: !0
                    }) : wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    chooseCoupon: function(t) {
        var a, o = t.currentTarget.dataset.item, s = t.currentTarget.dataset.index + "", i = !0, n = this, r = this.data.form.coupon, d = n.data.coupon_result;
        (r.some(function(t, a) {
            var l;
            if (t.id == o.id) return i = !1, r.splice(a, 1), d.splice(s, 1), n.setData((e(l = {}, "form.coupon", r), 
            e(l, "coupon_result", d), l)), !0;
        }), i) && (r.push({
            id: o.id,
            name: o.name,
            coupon_num: "1"
        }), d.push(s), n.setData((e(a = {}, "form.coupon", r), e(a, "coupon_result", d), 
        a)));
    },
    delCoupon: function(t) {
        var a, o = t.currentTarget.dataset.index;
        this.data.form.coupon.splice(o, 1), this.data.coupon_result.splice(o, 1), this.setData((e(a = {}, "form.coupon", this.data.form.coupon), 
        e(a, "coupon_result", this.data.coupon_result), a));
    },
    onCloneCoupon: function() {
        this.setData({
            coupon_show: !1
        });
    },
    changeCouponInput: function(t) {
        var a = t.currentTarget.dataset.index;
        this.setData(e({}, "form.coupon[" + a + "].coupon_num", t.detail.value));
    },
    onChangeLead: function(t) {
        this.setData(e({}, "form.is_lead", t.detail));
    },
    inputLead: function(t) {
        this.setData(e({}, "form.setting", t.detail));
    },
    inputShare: function(t) {
        var a = t.currentTarget.dataset.tag;
        this.setData(e({
            startPos: t.detail.cursor
        }, "form." + a, t.detail.value));
    },
    inputblurShare: function(t) {
        this.setData({
            startPos: t.detail.cursor
        });
    },
    addContent: function(t) {
        var e = this, a = t.currentTarget.dataset.name, o = t.currentTarget.dataset.index, s = t.currentTarget.dataset.num, i = "";
        1 == s && (i = "<<" + this.data.keywords.share_title[o] + ">>"), 2 == s && (i = "<<" + this.data.keywords.share_content[o] + ">>"), 
        3 == s && (i = "<<" + this.data.keywords.desc_title[o] + ">>"), 4 == s && (i = "<<" + this.data.keywords.desc_content[o] + ">>"), 
        console.log(i), setTimeout(function() {
            e.insertInputTxt(a, i, s);
        }, 200);
    },
    insertInputTxt: function(t, a, o) {
        var s = this.data.startPos, i = this.data.startPos, n = this.data.form[t];
        void 0 !== s && void 0 !== i || (s = n.length, i = n.length, console.log("startPos==========", s));
        var r = n.substring(0, s) + a + n.substring(i);
        this.setData(e({
            startPos: void 0
        }, "form." + t, r));
    },
    next: function() {
        var t = this;
        if (console.log(this.data.form), !this.data.form.title) return wx.showToast({
            title: "活动标题不能为空！",
            icon: "none",
            duration: 1e3
        }), !1;
        if (!this.data.has_one_goods.title) return wx.showToast({
            title: "请选择活动商品！",
            icon: "none",
            duration: 1e3
        }), !1;
        if (0 == this.data.form.has_many_level.length) return wx.showToast({
            title: "层级不能为空！",
            icon: "none",
            duration: 1e3
        }), !1;
        if (!this.data.form.start_time) return wx.showToast({
            title: "开始时间不能为空！",
            icon: "none",
            duration: 1e3
        }), !1;
        if (this.data.form.end_time && this.data.form.start_time >= this.data.form.end_time) return wx.showToast({
            title: "开始时间不能大于或者等于结束时间！",
            icon: "none",
            duration: 1e3
        }), !1;
        if (1 == this.data.form.menu_show && this.data.form.hide_num < 1) return wx.showToast({
            title: "展示设置->可参的团数不能少于1",
            icon: "none",
            duration: 1e3
        }), !1;
        if (this.data.form.has_many_level.some(function(e) {
            return e.member_num && e.expire_in ? 1 == e.member_num ? (wx.showToast({
                title: "拼团人数不能为1",
                icon: "none",
                duration: 1e3
            }), !0) : (t.setData({
                step: 2
            }), void console.log(t.data.step)) : (wx.showToast({
                title: "层级信息不完成！",
                icon: "none",
                duration: 1e3
            }), !0);
        }), !this.data.form.id) {
            var a = this.data.form.has_many_options;
            if (this.data.has_option) {
                for (var o = 0; o < a.length; o++) a[o].has_many_option_level = [];
                for (var s = 0; s < a.length; s++) for (var i = 0; i < this.data.form.has_many_level.length; i++) a[s].has_many_option_level.push({
                    group_price: "",
                    member_num: this.data.form.has_many_level[i].member_num
                });
                console.log(this.data.form);
            } else {
                a[0].has_many_option_level = [];
                for (var n = 0; n < this.data.form.has_many_level.length; n++) a[0].has_many_option_level.push({
                    group_price: "0",
                    member_num: this.data.form.has_many_level[n].member_num
                });
            }
            this.setData(e({}, "form.has_many_options", a));
        }
    },
    last: function() {
        this.setData({
            step: 1
        });
    },
    changeGroupPrice: function(t) {
        var a = t.currentTarget.dataset.index, o = t.currentTarget.dataset.index1;
        this.setData(e({}, "form.has_many_options[" + a + "].has_many_option_level[" + o + "].group_price", t.detail.value));
    },
    changeStock: function(t) {
        var a = t.currentTarget.dataset.index;
        this.setData(e({}, "form.has_many_options[" + a + "].stock", t.detail.value));
    },
    changeLimit: function(t) {
        var a = t.currentTarget.dataset.index;
        this.setData(e({}, "form.has_many_options[" + a + "].buy_limit", t.detail.value));
    },
    submitForm: function() {
        delete this.data.form.goods_name, delete this.data.form.loves, delete this.data.form.love_name, 
        delete this.data.form.is_open_integral, delete this.data.form.fight_groups_lottery, 
        delete this.data.form.fight_groups_lottery_info, console.log(this.data.form);
        var t = s.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.save");
        s._postNetWork({
            url: t,
            data: {
                form_data: this.data.form
            },
            success: function(t) {
                var e = t.data;
                1 == e.result ? wx.navigateTo({
                    url: "/packageC/o2o/storeManage/storeManage"
                }) : wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    }
});